package com.golxen.framework.common.server;


import com.golxen.framework.common.BuiltinPlugin;
import com.golxen.framework.common.GameParamPlugin;
import com.golxen.framework.common.server.eServerType;
import com.golxen.framework.netty.session.game.GameServerLinkedSet;
import com.golxen.framework.db.DBManager;
import com.golxen.framework.db.DbPlugin;
import com.golxen.framework.executor.ThreadPlugin;
import com.golxen.framework.manager.TempMgr;
import com.golxen.framework.netty.NettyServices;

import com.golxen.framework.redis.RedisPlugin;
import com.golxen.utils.YmlUtil;
import io.netty.channel.ChannelHandler;

import java.io.File;
import java.util.List;

public abstract class BaseServer {
    /**
     * 服务器类型
     */
    protected eServerType serverType;
    /**
     * 服务器编号
     */
    protected long serverId;

    public abstract List<TempMgr> getMgrList();

    protected NettyServices nettyServices;

    public boolean start() {

        //连接读取server中心服数据库的配置
        DbPlugin.start();

        //根据中心服配置各数据库的连接池
        DBManager.initGameDbConfig(serverType);
        RedisPlugin.start();
        BuiltinPlugin.getServerType().add(serverType);

        ThreadPlugin.start();

        GameParamPlugin.start();
//        ChannelConfigPlugin.start();

        return true;
    }


    protected boolean stop() {
        nettyServices.newStop();
        return true;
    }

    protected boolean initNetty(ChannelHandler handler, eServerType type) {

        nettyServices = NettyServices.start(YmlUtil.getInt("netty.game.port"), handler, "1>" + type.value);

        GameServerLinkedSet.init(YmlUtil.getInt("netty.game.agentCount"));

        return true;
    }

    public eServerType getServerType() {
        return serverType;
    }

    public void setServerType(eServerType serverType) {
        this.serverType = serverType;
    }

    public long getServerId() {
        return BuiltinPlugin.getServerId();
    }

    /**
     * 删除单个文件
     * 
     * @param sPath 被删除文件的文件名
     * @return 单个文件删除成功返回true，否则返回false
     */
    public static boolean deleteFile(String sPath) {
        boolean flag = false;
        File file = new File(sPath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }

}
